package com.zlc.platform.controller;


import cn.hutool.core.map.MapUtil;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.model.control.OnlineRequest;
import com.zlc.platform.model.OnlineEntity;
import com.zlc.platform.model.cache.CacheSystemEntity;
import com.zlc.platform.model.control.AdminControlEntity;
import com.zlc.platform.service.ControlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Api(tags = "系统监控")
@RestController
@Slf4j
@RequestMapping("/api/control")
public class ControlController {

    @Autowired
    private ControlService controlService;

    /**
     * 在线用户
     */
    @ApiOperation(value = "在线用户")
    @PostMapping(value = "/online")
    public PageVo<OnlineEntity> online(@RequestBody OnlineRequest request) {
        return controlService.queryOnlineUserList(request);
    }

    @ApiOperation(value = "查询系统监控")
    @PostMapping(value = "/queryAdminControlEntity")
    public AdminControlEntity queryAdminControlEntity() {
        return controlService.queryAdminControlEntity();
    }

    @ApiOperation(value = "查询系统监控SSE")
    @GetMapping(value = "/getAdminControl", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> getAdminControl() {
        return controlService.getAdminControl();
    }


    /**
     * 查询缓存系统列表
     */
    @ApiOperation(value = "查询缓存系统列表")
    @GetMapping(value = "/queryCacheSystemList")
    public List<CacheSystemEntity> queryCacheSystemList() {
        return controlService.queryCacheSystemList();
    }

    /**
     * 查询redis件
     */
    @ApiOperation(value = "查询redis件")
    @GetMapping(value = "/queryRedisKeys")
    public Set<Map<String,String>> queryRedisKeys(@RequestParam(value = "prefixKey") String prefixKey) {
        Set<String> strings = controlService.queryRedisKeys(prefixKey);
        return strings.stream().map(key -> MapUtil.of("key", key)).collect(Collectors.toSet());
    }

    /**
     * 获取redis值
     */
    @ApiOperation(value = "获取redis值")
    @GetMapping(value = "/getRedisValue")
    public String getRedisValue(@RequestParam(value = "key") String redisKey) {
        return controlService.getRedisValue(redisKey);
    }
}
